// ==UserScript==
// @name         Setting Dashboard
// @version      1.0
// @description  Setting Dashboard
// @author       Luo
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/jquery-ui.min.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/underscore.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/jsonform.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/InitSetting.js
// @match        http*://*/issues/?filter=*
// @match        http*://*/browse/*-*
// ==/UserScript==
var schema = {
    "schema": {
        "source": {
            "type": "object",
            "properties": {
                "host": {
                    "type": "string",
                    "title": "Source Host",
                    "required": true
                },
                "token": {
                    "type": "string",
                    "title": "Source Token",
                    "required": true
                },
                "searchMax": {
                    "type": "string",
                    "title": "Source Search Max",
                    "required": true
                }
            }
        },
        "target": {
            "type": "object",
            "properties": {
                "host": {
                    "type": "string",
                    "title": "Target Host",
                    "required": true
                },
                "token": {
                    "type": "string",
                    "title": "Target Token",
                    "required": true
                },
                "searchMax": {
                    "type": "string",
                    "title": "Target Search Max",
                    "required": true
                }
            }
        },
        "resources": {
            "type":"textarea",
            "title":"Resources",
        },
        "lavelMapping": {
            "type":"textarea",
            "title":"Lavel Mapping",
        }
    },
    "form": [{
            "type": "fieldset",
            "title": "Source",
            "items": [
                "source.host",
                "source.token",
                "source.searchMax"
            ]
        },
        {
            "type": "fieldset",
            "title": "Target",
            "items": [
                "target.host",
                "target.token",
                "target.searchMax"
            ]
        },
        {
            'key':'resources'
        },
        {
            'key':'lavelMapping'
        },
        {
            "type": "submit",
            "title": "Save",
            "htmlClass": "aui-button aui-button-primary aui-style"
        }
    ],
    "onSubmit": function(errors, values) {
        if (!errors) {
            // set to localStrong
            let config = JSON.parse(localStorage.getItem('config'));
			config = config ? config : {};
            Object.keys(values).forEach(function(key) {
                config[key] = values[key];
            });
			localStorage.setItem('config', JSON.stringify(config));
            console.log('Update setting success.');
            alert('Update setting success.');
        } else {
            console.error('Error', errors);
        }

        return false;
    },
    "params": {
        "fieldHtmlClass": "textfield text long-field"
    }
};
var config = JSON.parse(localStorage.getItem('config'));
try {
    if (config) {
        schema.value = {
            "source":{
                "host":config.source.host,
                "token":config.source.token,
                "searchMax": config.source.searchMax
            },
            "target":{
                "host":config.target.host,
                "token":config.target.token,
                "searchMax": config.target.searchMax
            },
            "resources":config.resources,
            "lavelMapping":config.lavelMapping
        };
    }
} catch(e) {
    console.error('##Can not load exist config!', e);
}
var formSetting = new InitSetting('config', schema);
